﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;

namespace TestApplication.usercontrols
{
    /// <summary>
    /// Interaction logic for ObjectControl.xaml
    /// </summary>
    public partial class ObjectControl : UserControl
    {
        private GeometryModel3D mGeometry;
        private bool mDown;
        private Point mLastPos;

        public ObjectControl()
        {
            InitializeComponent();
     /*       mGeometry = this. GuitarViewBox.Model;
            mGeometry.Transform = new Transform3DGroup();
            this. GuitarViewBox.ModelGroup.Children.Add(mGeometry);*/

        }

        private void ZoomIn_Click(object sender, RoutedEventArgs e)
        {
    //        this.GuitarViewBox.ViewPortCamera.Position = new Point3D(
     //           this.GuitarViewBox.ViewPortCamera.Position.X,
    //            this.GuitarViewBox.ViewPortCamera.Position.Y,
    //            this.GuitarViewBox.ViewPortCamera.Position.Z - 40);


        }
       
        private void ZoomOut_Click(object sender, RoutedEventArgs e)
        {
     //       this.GuitarViewBox.ViewPortCamera.Position = new Point3D(
    //        this.GuitarViewBox.ViewPortCamera.Position.X,
     //       this.GuitarViewBox.ViewPortCamera.Position.Y,
    //        this.GuitarViewBox.ViewPortCamera.Position.Z + 40);
        }/*


        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed) return;
            mDown = true;
            Point pos = Mouse.GetPosition(this.GuitarViewBox.Viewport3D);
            mLastPos = new Point(
                    pos.X - this.GuitarViewBox.Viewport3D.ActualWidth / 2,
                    this.GuitarViewBox.Viewport3D.ActualHeight / 2 - pos.Y);
        }

        private void Grid_MouseUp(object sender, MouseButtonEventArgs e)
        {
            mDown = false;
        }

        private void Grid_MouseMove(object sender, MouseEventArgs e)
        {
            if (!mDown) return;
            Point pos = Mouse.GetPosition(this.GuitarViewBox.Viewport3D);
            Point actualPos = new Point(
                    pos.X - this.GuitarViewBox.Viewport3D.ActualWidth / 2,
                    this.GuitarViewBox.Viewport3D.ActualHeight / 2 - pos.Y);
            double dx = actualPos.X - mLastPos.X;
            double dy = actualPos.Y - mLastPos.Y;
            double mouseAngle = 0;

            if (dx != 0 && dy != 0)
            {
                mouseAngle = Math.Asin(Math.Abs(dy) /
                    Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2)));
                if (dx < 0 && dy > 0) mouseAngle += Math.PI / 2;
                else if (dx < 0 && dy < 0) mouseAngle += Math.PI;
                else if (dx > 0 && dy < 0) mouseAngle += Math.PI * 1.5;
            }
            else if (dx == 0 && dy != 0)
            {
                mouseAngle = Math.Sign(dy) > 0 ? Math.PI / 2 : Math.PI * 1.5;
            }
            else if (dx != 0 && dy == 0)
            {
                mouseAngle = Math.Sign(dx) > 0 ? 0 : Math.PI;
            }

            double axisAngle = mouseAngle + Math.PI / 2;

            Vector3D axis = new Vector3D(
                    Math.Cos(axisAngle) * 4,
                    Math.Sin(axisAngle) * 4, 0);

            double rotation = 0.02 *
                    Math.Sqrt(Math.Pow(dx, 2) + Math.Pow(dy, 2));

            Transform3DGroup group = mGeometry.Transform as Transform3DGroup;
            QuaternionRotation3D r =
                 new QuaternionRotation3D(
                 new Quaternion(axis, rotation * 180 / Math.PI));
            group.Children.Add(new RotateTransform3D(r));

            mLastPos = actualPos;
        } */
    }
}
